﻿using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Json;

namespace WelcomeRESTJSON
{
    public partial class WelcomeRESTJSONForm : Form
    {
        private HttpClient client = new HttpClient();
        private string port = "8573";

        public WelcomeRESTJSONForm()
        {
            InitializeComponent();
        }        

        private async void submitButton_Click(object sender, EventArgs e)
        {
            string result = await client.GetStringAsync(
                        new Uri(string.Format("http://localhost:{0}/WelcomeService.svc/welcome/{1}", port, nameTextBox.Text)));

            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(TextMessage));

            TextMessage message = ( TextMessage ) jsonSerializer.ReadObject(new MemoryStream(Encoding.Unicode.GetBytes(result)));

            MessageBox.Show(message.Message, "Welcome");
        }
    }

    [Serializable]
    public class TextMessage
    {
        public string Message;
    }
}
